package articleenum

import (
	"yunj/pkg/yunj/core/enum"
)

// 定义常量
const (
	// 时间倒序排列
	SORT_RULE_RECENT string = "recent"

	// 浏览量倒序排列
	SORT_RULE_HOT string = "hot"

	// 搜索关键词得分
	SORT_RULE_KEYWORDS_SCORE string = "keywords_score"
)

// 实现EnumInerface接口
type SortRule struct {
	enum.Enum[string]
}

func (e *SortRule) AllConstAttrs() map[string]map[string]any {
	return map[string]map[string]any{
		SORT_RULE_RECENT: {
			"desc":  "时间倒序排列",
			"sort":  1,
			"color": "#16baaa",
		},
		SORT_RULE_HOT: {
			"desc":  "浏览量倒序排列",
			"sort":  2,
			"color": "#ffb800",
		},
		SORT_RULE_KEYWORDS_SCORE: {
			"desc":  "搜索关键词得分",
			"sort":  3,
			"color": "#ff5722",
		},
	}
}

// instanceCache 存储已经创建的实例
var sortRuleInstanceCache = make(map[string]*SortRule)

// 创建枚举
// 根据value获取枚举
func SortRuleByValue(value string) *SortRule {
	if instance, exists := sortRuleInstanceCache[value]; exists {
		return instance
	}
	e := &SortRule{}
	enum.SetEnumInstance(&e.Enum, e, value)
	sortRuleInstanceCache[value] = e
	return e
}

var SortRuleEnum *SortRule = func() *SortRule {
	e := &SortRule{}
	enum.SetEnumInstance(&e.Enum, e)
	return e
}()
